//
//  WLExtractScoreVController.m
//  WanLian
//
//  Created by muzhuang on 2018/10/11.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLExtractScoreVController.h"
#import "WLWebViewController.h"
#import "WLBindingINTAlertView.h"

@interface WLExtractScoreVController ()
{
    UITextField *textNum;
    UIButton *btnSure;
    UILabel *_labScore;
}

@end

@implementation WLExtractScoreVController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"兑换INT积分";
    
    WLUserInfo *user = [[WLUseManager shareUser] getUserInformation];
    if (user.intAccount==nil) {
        [self showNoAccountView];
    }
    else
    {
        [self showAccountView:user.intAccount];
    }
    
    
}

- (void)showNoAccountView
{
    for (UIView *subView in self.view.subviews) {
        [subView removeFromSuperview];
    }
    UILabel *labAccount = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(20), 200, 30)];
    labAccount.font = [WLCommonTool fontTypePingFangSCMedium:20];
    labAccount.textColor = wlBlackColor;
    labAccount.text = @"INT账户地址：";
    [self.view addSubview:labAccount];
    
    UILabel *labNoAcc = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(20+5)+30, 240, WLScaleX(20))];
    labNoAcc.font = [WLCommonTool fontTypePingFangSCMedium:14];
    labNoAcc.textColor = wlDarkGrayColor;
    labNoAcc.text = @"您还没有绑定INT账户地址哦~";
    [self.view addSubview:labNoAcc];
    
    UIButton *btnBinding = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(20+5+20+15)+30, WLScaleX(140), WLScaleX(30))];
    btnBinding.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
    [btnBinding setTitleColor:wlblueColor forState:UIControlStateNormal];
    [btnBinding setTitle:@"绑定INT账户" forState:UIControlStateNormal];
    [btnBinding addTarget:self action:@selector(bindingIntAccAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBinding];
    btnBinding.layer.cornerRadius = 5;
    btnBinding.layer.masksToBounds = YES;
    btnBinding.layer.borderWidth = 1;
    btnBinding.layer.borderColor = wlblueColor.CGColor;
    
    UIButton *btnCreatTip = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16+140+10), CGRectGetMaxY(btnBinding.frame)-WLScaleX(20), 170, WLScaleX(20))];
    btnCreatTip.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
    [btnCreatTip setTitleColor:wlblueColor forState:UIControlStateNormal];
    [btnCreatTip setTitle:@"创建一个新的INT账户?" forState:UIControlStateNormal];
    [btnCreatTip addTarget:self action:@selector(createIntTipAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCreatTip];
    UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16+140+15), CGRectGetMaxY(btnBinding.frame), 160, 1)];
    lineBottom.backgroundColor = wlblueColor;
    [self.view addSubview:lineBottom];
    
    UILabel *labNum = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(btnBinding.frame)+WLScaleX(20), 150, 30)];
    labNum.font = [WLCommonTool fontTypePingFangSCMedium:20];
    labNum.textColor = wlBlackColor;
    labNum.text = @"INT数量：";
    [self.view addSubview:labNum];
    
    textNum = [[UITextField alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(labNum.frame)+WLScaleX(5), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(30))];
//    textNum.delegate = self;
    textNum.keyboardType = UIKeyboardTypeDecimalPad;
    textNum.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入兑换额度" attributes:@{NSFontAttributeName:[WLCommonTool fontTypePingFangSCRegular:14],NSForegroundColorAttributeName:wlUnderlineColor}];
    [self.view addSubview:textNum];
    
    UIView *lineText = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(textNum.frame), WIDTH_SCREEN_WL-WLScaleX(32), 1)];
    lineText.backgroundColor = wlUnderlineColor;
    [self.view addSubview:lineText];
    
    UILabel *labMax = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(lineText.frame)+WLScaleX(2), 200, 20)];
    labMax.font = [WLCommonTool fontTypePingFangSCMedium:11];
    labMax.textColor = wlUnderlineColor;
    
    if (self.enableInt !=nil&&[self.enableInt isKindOfClass:[NSNull class]]==NO) {
        labMax.text = [NSString stringWithFormat:@"(最大可兑换%@个INT)",self.enableInt];
    }
    else
    {
        labMax.text = @"(最大可兑换0个INT)";
    }
    [self.view addSubview:labMax];
    
//    UIButton *btnRule = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-30, CGRectGetMidY(labMax.frame)-15, 30, 30)];
//    [btnRule setImage:[UIImage imageNamed:@"help_gray"] forState:UIControlStateNormal];
//    [btnRule addTarget:self action:@selector(INTRuleAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btnRule];
    UIButton *btnRule = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-100, CGRectGetMidY(labMax.frame)-15, 100, 30)];
    [btnRule setImage:[UIImage imageNamed:@"help_gray"] forState:UIControlStateNormal];
    [btnRule setTitle:@"INT兑换规则 " forState:UIControlStateNormal];
    btnRule.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:12];
    [btnRule setTitleColor:wlLightTextColor forState:UIControlStateNormal];
    [btnRule setTitleEdgeInsets:UIEdgeInsetsMake(0, - btnRule.imageView.image.size.width, 0, btnRule.imageView.image.size.width)];
    [btnRule setImageEdgeInsets:UIEdgeInsetsMake(0, btnRule.titleLabel.bounds.size.width, 0, -btnRule.titleLabel.bounds.size.width)];
    [btnRule addTarget:self action:@selector(INTRuleAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnRule];
    
    UIView *underLine = [[UIView alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-95, CGRectGetMaxY(btnRule.frame)-7, 70, 1)];
    underLine.backgroundColor = wlUnderlineColor;
    [self.view addSubview:underLine];
    
//    UILabel *labRule = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-30-120, CGRectGetMinY(labMax.frame), 120, 20)];
//    labRule.textAlignment = NSTextAlignmentRight;
//    labRule.font = [WLCommonTool fontTypePingFangSCRegular:12];
//    labRule.textColor = wlLightTextColor;
//    labRule.attributedText = [[NSAttributedString alloc] initWithString:@"INT兑换规则" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
//    [self.view addSubview:labRule];
    
    UIButton *btnSure = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(labMax.frame)+WLScaleX(36), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(47))];
    btnSure.backgroundColor = wlUnderlineColor;
    btnSure.enabled = NO;
//    CAGradientLayer *gradientBtn = [CAGradientLayer layer];
//    gradientBtn.frame = btnSure.bounds;  // 设置显示的frame
//    gradientBtn.colors = @[(__bridge id)wlUIColorRGB(0x1A59E2).CGColor,(__bridge id)wlUIColorRGB(0x2292F5).CGColor]; // 设置渐变颜色
//    gradientBtn.startPoint = CGPointMake(0, 0);
//    gradientBtn.endPoint = CGPointMake(1, 0);
//    [btnSure.layer addSublayer:gradientBtn];
    
    btnSure.layer.cornerRadius = 5;//WLScaleX(5);
    btnSure.layer.masksToBounds = YES;
    btnSure.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:16];
    [btnSure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSure setTitle:@"确认兑换" forState:UIControlStateNormal];
    [btnSure addTarget:self action:@selector(sureExtractAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSure];
}

- (void)showAccountView:(NSString *)account
{
    for (UIView *subView in self.view.subviews) {
        [subView removeFromSuperview];
    }
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(20), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(28))];
    topView.backgroundColor = wlUIColorRGB(0xF2F2F2);
    topView.layer.cornerRadius = 5;
    topView.layer.masksToBounds = YES;
    [self.view addSubview:topView];
    
    UIImageView *ivTip = [[UIImageView alloc] initWithFrame:CGRectMake(WLScaleX(10), WLScaleX(6), WLScaleX(16), WLScaleX(16))];
    ivTip.image = [UIImage imageNamed:@"lab_headTip"];
    [topView addSubview:ivTip];
    
    UILabel *labScore = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(32), WLScaleX(4), topView.bounds.size.width-WLScaleX(48), WLScaleX(20))];
    labScore.font = [WLCommonTool fontTypePingFangSCRegular:11];
    labScore.textColor = wlLightTextColor;
    [topView addSubview:labScore];
    _labScore = labScore;
    
    NSMutableAttributedString *attStr = [WLCommonTool getINTDescriptionWithINTNum:self.enableScore];
    labScore.attributedText = attStr;
    
    UILabel *labAccount = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(20+28+20), 200, 30)];
    labAccount.font = [WLCommonTool fontTypePingFangSCMedium:20];
    labAccount.textColor = wlBlackColor;
    labAccount.text = @"INT账户地址：";
    [self.view addSubview:labAccount];
    
//    WLUserInfo *user = [[WLUseManager shareUser] getUserInformation];
    UILabel *labNoAcc = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(68)+30, WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(45))];
    labNoAcc.numberOfLines = 2;
    labNoAcc.font = [WLCommonTool fontTypePingFangSCMedium:14];
    labNoAcc.textColor = wlDarkGrayColor;
    labNoAcc.text = account;
    [self.view addSubview:labNoAcc];
    
    UIView *lineAcc = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(labNoAcc.frame), WIDTH_SCREEN_WL-WLScaleX(32), 1)];
    lineAcc.backgroundColor = wlUnderlineColor;
    [self.view addSubview:lineAcc];
    
    UIButton *btnBinding = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(68+45+10)+30, WLScaleX(140), WLScaleX(30))];
    btnBinding.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
    [btnBinding setTitleColor:wlLightTextColor forState:UIControlStateNormal];
    [btnBinding setTitle:@"解绑INT账户" forState:UIControlStateNormal];
    [btnBinding addTarget:self action:@selector(bindingOpenAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBinding];
    btnBinding.layer.cornerRadius = 5;
    btnBinding.layer.masksToBounds = YES;
    btnBinding.layer.borderWidth = 1;
    btnBinding.layer.borderColor = wlLightTextColor.CGColor;
    
    UILabel *labNum = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(btnBinding.frame)+WLScaleX(20), 150, 30)];
    labNum.font = [WLCommonTool fontTypePingFangSCMedium:20];
    labNum.textColor = wlBlackColor;
    labNum.text = @"INT数量：";
    [self.view addSubview:labNum];
    
    textNum = [[UITextField alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(labNum.frame)+WLScaleX(5), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(30))];
    //    textNum.delegate = self;
    textNum.keyboardType = UIKeyboardTypeDecimalPad;
    textNum.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入兑换额度" attributes:@{NSFontAttributeName:[WLCommonTool fontTypePingFangSCRegular:14],NSForegroundColorAttributeName:wlUnderlineColor}];
    [textNum addTarget:self action:@selector(textNumChanged) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:textNum];
    
    UIView *lineText = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(textNum.frame), WIDTH_SCREEN_WL-WLScaleX(32), 1)];
    lineText.backgroundColor = wlUnderlineColor;
    [self.view addSubview:lineText];
    
    UILabel *labMax = [[UILabel alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(lineText.frame)+WLScaleX(2), 200, 20)];
    labMax.font = [WLCommonTool fontTypePingFangSCMedium:11];
    labMax.textColor = wlUnderlineColor;
    
    if (self.enableInt !=nil&&[self.enableInt isKindOfClass:[NSNull class]]==NO) {
        labMax.text = [NSString stringWithFormat:@"(最大可兑换%@个INT)",self.enableInt];
    }
    else
    {
        labMax.text = @"(最大可兑换0个INT)";
    }
    [self.view addSubview:labMax];
    
//    UIButton *btnRule = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-30, CGRectGetMidY(labMax.frame)-15, 30, 30)];
    UIButton *btnRule = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-100, CGRectGetMidY(labMax.frame)-15, 100, 30)];
    [btnRule setImage:[UIImage imageNamed:@"help_gray"] forState:UIControlStateNormal];
    [btnRule setTitle:@"INT兑换规则 " forState:UIControlStateNormal];
    btnRule.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:12];
    [btnRule setTitleColor:wlLightTextColor forState:UIControlStateNormal];
    [btnRule setTitleEdgeInsets:UIEdgeInsetsMake(0, - btnRule.imageView.image.size.width, 0, btnRule.imageView.image.size.width)];
    [btnRule setImageEdgeInsets:UIEdgeInsetsMake(0, btnRule.titleLabel.bounds.size.width, 0, -btnRule.titleLabel.bounds.size.width)];
    [btnRule addTarget:self action:@selector(INTRuleAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnRule];
    
    UIView *underLine = [[UIView alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-95, CGRectGetMaxY(btnRule.frame)-7, 70, 1)];
    underLine.backgroundColor = wlUnderlineColor;
    [self.view addSubview:underLine];
//    UILabel *labRule = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-WLScaleX(10)-30-120, CGRectGetMinY(labMax.frame), 120, 20)];
//    labRule.textAlignment = NSTextAlignmentRight;
//    labRule.font = [WLCommonTool fontTypePingFangSCRegular:12];
//    labRule.textColor = wlLightTextColor;
//    labRule.attributedText = [[NSAttributedString alloc] initWithString:@"INT兑换规则" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
//    [self.view addSubview:labRule];
    
    btnSure = [[UIButton alloc] initWithFrame:CGRectMake(WLScaleX(16), CGRectGetMaxY(labMax.frame)+WLScaleX(36), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(47))];
    btnSure.backgroundColor = wlUnderlineColor;
    btnSure.enabled = NO;
//    CAGradientLayer *gradientBtn = [CAGradientLayer layer];
//    gradientBtn.frame = btnSure.bounds;  // 设置显示的frame
//    gradientBtn.colors = @[(__bridge id)wlUIColorRGB(0x1A59E2).CGColor,(__bridge id)wlUIColorRGB(0x2292F5).CGColor]; // 设置渐变颜色
//    gradientBtn.startPoint = CGPointMake(0, 0);
//    gradientBtn.endPoint = CGPointMake(1, 0);
//    [btnSure.layer addSublayer:gradientBtn];
    
    btnSure.layer.cornerRadius = 5;//WLScaleX(5);
    btnSure.layer.masksToBounds = YES;
    btnSure.titleLabel.font = [WLCommonTool fontTypePingFangSCMedium:16];
    [btnSure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSure setTitle:@"确认兑换" forState:UIControlStateNormal];
    [btnSure addTarget:self action:@selector(sureExtractAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnSure];
}

- (void)textNumChanged//保留小数点后6位
{
    NSString *numStr = textNum.text;
    NSRange dotRange = [numStr rangeOfString:@"."];
    if (dotRange.location !=NSNotFound) {
        if (numStr.length-dotRange.location>6&&textNum.markedTextRange == nil)
        {
            textNum.text = [numStr substringToIndex:dotRange.location+7];
        }
    }
    if ([textNum.text floatValue]<=_enableInt.floatValue&&textNum.text.floatValue>0) {
        btnSure.enabled = YES;
        [WLCommonTool addGradientColor:btnSure fromColor:wlblueColor toColor:wlUIColorRGB(0x2292F5)];
    }
    else
    {
        btnSure.enabled = NO;
        NSArray *arrLayers = [NSArray arrayWithArray:btnSure.layer.sublayers];
        for (CALayer *subLayer in arrLayers) {
            if ([subLayer isKindOfClass:[CAGradientLayer class]])
            {
                [subLayer removeFromSuperlayer];
            }
        }
        btnSure.backgroundColor = wlUnderlineColor;
//        btnSure.enabled = YES;
//        [WLCommonTool addGradientColor:btnSure fromColor:wlblueColor toColor:wlUIColorRGB(0x2292F5)];
    }
}

- (void)INTRuleAction
{
    WLWebViewController *webView = [[WLWebViewController alloc] init];
//    webView.urlStr = @"http://47.75.195.106/help/helpdetail.html?detail=16";
    webView.urlStr = [NSString stringWithFormat:@"%@/help/helpdetail.html?detail=16",[WLCommonTool getWebHost]];
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)sureExtractAction
{
    [self.view endEditing:YES];
    if (textNum.text.length<1) {
        [self showHUDMessage:@"请输入兑换额度"];
    }
    else
    {
        WLWkSelf
        NSDictionary *paramsp = @{@"num":textNum.text};
        __block NSString *tiquNum = [textNum.text copy];
        textNum.text = nil;
//        NSDictionary *paramsp = @{@"conditions":conditions};
        [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"score/app/score/changeint" withParamsDic:paramsp success:^(NSDictionary *successDict) {
            __strong typeof(self) strongSelf = weakSelf;
            NSString *strMsg = successDict[@"message"];
            if (strMsg != nil&&[strMsg isKindOfClass:[NSNull class]]==NO)
            {
                [weakSelf showHUDMessage:strMsg];
            }
            
            float INTFloat = [self.enableScore floatValue];
            INTFloat -=  tiquNum.floatValue;
            NSMutableAttributedString *attStr = [WLCommonTool getINTDescriptionWithINTNum:[NSString stringWithFormat:@"%.6f", INTFloat]];
            strongSelf ->_labScore.attributedText = attStr;
        } failure:^(NSString *errorMsg, NSInteger errorTag) {
            [weakSelf showHUDMessage:errorMsg];
        } error:^(NSError *error) {
    
        } inView:self.view];
    }
}

- (void)bindingOpenAction
{
    WLWkSelf
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"user/modify/untiedint" withParamsDic:nil success:^(NSDictionary *successDict) {
        [weakSelf showNoAccountView];
        WLUserInfo *user = [[WLUseManager shareUser] getUserInformation];
        user.intAccount = nil;
        [[WLUseManager shareUser] updateUserValue:user];
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
    } error:^(NSError *error) {
        
    } inView:self.view];
}

#pragma mark - no int account
- (void)bindingIntAccAction
{
    WLWkSelf
    WLBindingINTAlertView *bindingView = [[WLBindingINTAlertView alloc] init];
    bindingView.okAction = ^(NSString * _Nonnull strAddr) {
        [weakSelf showAccountView:strAddr];
    };
    [bindingView show];
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];
//    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//
//    }];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
//    }]];
//    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//
//    }]];
//    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)createIntTipAction
{
    WLWebViewController *webView = [[WLWebViewController alloc] init];
//    webView.urlStr = @"http://47.75.195.106/help/createint.html";
    webView.urlStr = [NSString stringWithFormat:@"%@/help/createint.html",[WLCommonTool getWebHost]];
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)showTextAlertView
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
